﻿using Anke.SHManage.BLL;
using Anke.SHManage.BLL.FixedAssetsBLL;
using Anke.SHManage.Model.EasyUIModel;
using Anke.SHManage.Model.FixedAssetsModel;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.FixedAssets.Controllers
{
    /// <summary>
    /// 固定资产仓库维护
    /// plq
    /// </summary>
    public class FixedAssetsStorageController : Controller
    {
        //
        // GET: /FixedAssets/FixedAssetsStorage/

        /// <summary>
        /// 固定资产仓库维护页
        /// </summary>
        /// <returns></returns>
        public ActionResult AssetsStorage()
        {
            //2019-10-31 plq 固定资产仓库维护新增、修改、启/禁用权限配置
            //获取用户最大统计权限
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            this.ViewData["em"] = em;
            UserOperateContext userOperateContext = new UserOperateContext();
            bool Insert = userOperateContext.getGongneng((int)E_FixedAssetsPermission.固定资产仓库新增修改);//判断登录人有无“新增修改”资产仓库权限
            if (Insert)
            {
                this.ViewData["RolePermissionInsert"] = "Insert";//登录人有新增修改资产仓库权限
            }
            bool Delete = UserOperateContext.Current.getGongneng((int)E_FixedAssetsPermission.固定资产仓库启禁用);//判断登录人有无“启禁用”资产仓库权限
            if (Delete)
            {
                this.ViewData["RolePermissionDelete"] = "Delete";//登录人有“启禁用”资产仓库权限
            }
            return View("AssetsStorage");
        }


        #region 固定资产使用部门/分站（当前仓库）树

        /// <summary>
        /// 加载所有固定资产仓库树-不筛选是否有效
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllAssetsStorageTree()
        {
            try
            {
                List<FixedAssetsStorageInfo> list = new FixedAssetsStorageBLL().GetAllStorageList();
                List<Model.EasyUIModel.TreeNode> listTree = new FixedAssetsStorageBLL().ToTreeNodes(list);
                return Json(listTree);
            }
            catch
            {
                return this.Json("");
            }
        }

        //获取所有固定资产类型树--筛选已禁用
        public ActionResult GetActiveAssetsStorageTree()
        {
            try
            {
                List<FixedAssetsStorageInfo> list = new FixedAssetsStorageBLL().GetActiveAssetsStorageList();
                List<TreeNode> listTree = new FixedAssetsStorageBLL().ToTreeNodes(list);
                return Json(listTree, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        #endregion

        /// <summary>
        /// 新增固定资产仓库
        /// </summary>
        /// <returns></returns>
        public ActionResult AddAssetsStorage()
        {
            string PID = Request.Form["PID"].ToString(); //父级编码
            string sName = Request.Form["StroageName"].ToString(); //仓库名
            FixedAssetsStorageBLL bll = new FixedAssetsStorageBLL();
            if (bll.AddAssetsStorage(PID, sName))
            {
                return Json(new { Result = "OK", Message = "新增资产仓库成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "新增资产仓库失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

        }

        /// <summary>
        /// 修改仓库名称
        /// </summary>
        /// <returns></returns>
        public ActionResult EditAssetsStorage()
        {
            string SID = Request.Form["SID"].ToString(); //仓库编码
            string sName = Request.Form["StroageName"].ToString(); //仓库名称
            FixedAssetsStorageBLL bll = new FixedAssetsStorageBLL();
            if (bll.EditAssetsStorage(SID, sName))
            {
                return Json(new { Result = "OK", Message = "修改资产仓库名称成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "修改资产仓库名称失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

        }

        /// <summary>
        /// 禁用资产仓库
        /// </summary>
        /// <returns></returns>
        public ActionResult DisableAssetsStorage()
        {
            string SID = Request.Form["SID"].ToString();
            FixedAssetsStorageBLL bll = new FixedAssetsStorageBLL();
            if (bll.DisableAssetsStorage(SID))
            {
                return Json(new { Result = "OK", Message = "禁用资产仓库成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "禁用资产仓库失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

        }

        /// <summary>
        /// 启用资产仓库
        /// </summary>
        /// <returns></returns>
        public ActionResult EnableAssetsStorage()
        {
            string SID = Request.Form["SID"].ToString();
            FixedAssetsStorageBLL bll = new FixedAssetsStorageBLL();
            if (bll.EnableAssetsStorage(SID))
            {
                return Json(new { Result = "OK", Message = "启用资产仓库成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "启用资产仓库失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }

        }




    }
}
